package org.springblade.modules.repair.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ImageUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.repair.entity.MaintainCheckHistory;
import org.springblade.modules.repair.entity.MaintainCheckRecord;
import org.springblade.modules.repair.entity.MaintainPlanContent;
import org.springblade.modules.repair.entity.PlanHistoryImage;
import org.springblade.modules.repair.enums.MaintainPlanContentEnum;
import org.springblade.modules.repair.mapper.PlanHistoryImageMapper;
import org.springblade.modules.repair.service.IMaintainCheckHistoryService;
import org.springblade.modules.repair.service.IMaintainPlanContentService;
import org.springblade.modules.repair.service.MaintainCheckRecordService;
import org.springblade.modules.repair.service.PlanHistoryImageService;
import org.springblade.modules.repair.utils.WatermarkImgUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@AllArgsConstructor
@Service
public class PlanHistoryImageServiceImpl extends BaseServiceImpl<PlanHistoryImageMapper, PlanHistoryImage> implements PlanHistoryImageService {

	private final IMaintainCheckHistoryService checkHistoryService;
	private final MaintainCheckRecordService checkRecordService;
	private final IMaintainPlanContentService planContentService;

	@Override
	public Boolean saveAllImage(List<PlanHistoryImage> entityList) {
		entityList = entityList.stream().filter(e -> StringUtil.isNotBlank(e.getUrl())).collect(Collectors.toList());
		if(Func.isEmpty(entityList)){
			return Boolean.TRUE;
		}
		return super.saveBatch(entityList);
	}

	@Override
	@TenantIgnore
	public IPage<PlanHistoryImage> selectPage(IPage<PlanHistoryImage> page, PlanHistoryImage entity) {
		return page.setRecords(baseMapper.selectPlanHistoryImagePage(page,entity));
	}

	@Override
	public List<PlanHistoryImage> selectList(PlanHistoryImage entity) {
		return baseMapper.selectImageList(entity);
	}

	@Override
	public PlanHistoryImage selectDetail(Long id, String url) {
		return baseMapper.selectDetail(id,url);
	}

	@Override
	public Boolean removeByPlan(Long planId) {

		MaintainPlanContent planContent = planContentService.getById(planId);
		if(MaintainPlanContentEnum.MAINTAIN_BY.getId() == planContent.getTypeStatus()){
			List<MaintainCheckHistory> checkHistories = checkHistoryService.listByPlanId(planId);
			if(Func.isNotEmpty(checkHistories)){
				List<String> imageUrlList = new ArrayList<>();

				List<Long> checkHistoryIds = checkHistories.stream().map(MaintainCheckHistory::getId).collect(Collectors.toList());
				List<MaintainCheckRecord> checkRecords = checkRecordService.listByCheckHistoryIds(checkHistoryIds);
				if(Func.isNotEmpty(checkRecords)){
					Map<Long,List<MaintainCheckRecord>> historyGroup = checkRecords.stream().collect(Collectors.groupingBy(MaintainCheckRecord::getCheckHistoryId));
					historyGroup.forEach((o1,o2) -> {
						Map<Long,List<MaintainCheckRecord>> userGroup = o2.stream().collect(Collectors.groupingBy(MaintainCheckRecord::getUserId));
						userGroup.forEach((k,v) -> {
							List<MaintainCheckRecord> userRecords = v.stream().sorted(Comparator.comparing(MaintainCheckRecord::getCreateTime).reversed()).collect(Collectors.toList());
							MaintainCheckRecord record = userRecords.stream().findFirst().orElse(null);
							if(Func.isNotEmpty(record) && Func.isNotEmpty(record.getRecordDataValue())){
								imageUrlList.addAll(WatermarkImgUtils.findImgVideoUrl(record.getRecordDataValue().toString()));
							}
						});
					});
				}
				checkHistories.forEach(e -> imageUrlList.addAll(WatermarkImgUtils.findImgVideoUrl(e.getRecordData().toString())));
				if(Func.isNotEmpty(imageUrlList)){
					return super.remove(Wrappers.<PlanHistoryImage>lambdaQuery().eq(PlanHistoryImage::getImgFrom,3).eq(PlanHistoryImage::getPlanId,planId).notIn(PlanHistoryImage::getUrl,imageUrlList));
				}else {
					return super.remove(Wrappers.<PlanHistoryImage>lambdaQuery().eq(PlanHistoryImage::getImgFrom,3).eq(PlanHistoryImage::getPlanId,planId));
				}
			}
		}
		return Boolean.TRUE;
	}
}
